home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / disk / callDisk.c next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  6.3 KB  |  252 lines

  1. /*
  2.  *   $RCSfile: callDisk.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:40 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37. #include "mutex_compile.h"
  38.  
  39. #include "sysdefs.h"
  40. #include "ess.h"
  41. #include "checking.h"
  42. #include "trace.h"
  43. #include "error.h"
  44. #include "list.h"
  45. #include "tid.h"
  46. #include "io.h"
  47. #include "lock.h"
  48. #include "object.h"
  49. #include "msgdefs.h"
  50. #include "disk.h"
  51. #include "thread.h"
  52. #include "semaphore.h"
  53. #include "latch.h"
  54. #include "link.h"
  55. #include "bf.h"
  56. #include "volume.h"
  57. #include "bitvec.h"
  58. #include "threadstate.h"
  59. #include "disk_funcs.h"
  60. #include "msg_funcs.h"
  61. #include "thread_globals.h"
  62. #include "stat_globals.h"
  63. #include "sharedmem_globals.h"
  64. #include "queue_consist.h"
  65.  
  66.  
  67. #ifdef TIME_CALLDISK
  68. #define HIST 50
  69. struct timeval hist[HIST];
  70. struct timeval avghist;
  71. int avgcount;
  72. static struct timezone timez;
  73. static int hist_index = 0;
  74.  
  75. void
  76. btime(
  77.     struct timeval *beginning
  78. )
  79. {
  80.     if(gettimeofday(beginning, &timez)<0) {
  81.         SM_ERROR(TYPE_FATAL ,errno);
  82.     }
  83. }
  84.  
  85. void
  86. etime(
  87.     struct timeval *beginning
  88. )
  89. {
  90.     struct timeval end, diff;
  91.     register long usec;
  92.  
  93.     if(gettimeofday(&end, &timez)<0) {
  94.         SM_ERROR(TYPE_FATAL ,errno);
  95.     }
  96.  
  97.     usec = (end.tv_usec + 1000000) - beginning->tv_usec;
  98.  
  99.     if(usec>1000000) {
  100.         usec -= 1000000;
  101.     } else {
  102.         end.tv_sec--;
  103.     }
  104.     diff.tv_usec = usec;
  105.  
  106.     diff.tv_sec = end.tv_sec - beginning->tv_sec;
  107.  
  108.     hist[hist_index].tv_sec = diff.tv_sec;
  109.     hist[hist_index].tv_usec = diff.tv_usec;
  110.     if(hist[hist_index].tv_usec >= 1000000) {
  111.         hist[hist_index].tv_usec -= 1000000;
  112.         hist[hist_index].tv_sec ++;
  113.     }
  114.     avghist.tv_sec += diff.tv_sec;
  115.     avghist.tv_usec += diff.tv_usec;
  116.     if(avghist.tv_usec >= 1000000) {
  117.         avghist.tv_usec -= 1000000;
  118.         avghist.tv_sec ++;
  119.     }
  120.     avgcount++;
  121.     hist_index++;
  122.     hist_index %= HIST;
  123. }
  124. #endif TIME_CALLDISK
  125.  
  126. #include "queue_globals.h"
  127.  
  128. int
  129. callDisk ( 
  130.     VOLREC             *volRec,
  131.     DISKMSG            *message
  132. )
  133. {
  134. #ifdef TIME_CALLDISK
  135.     struct timeval beginning; 
  136. #endif TIME_CALLDISK
  137.  
  138.     extern int     serv_v_ops;
  139.     extern int    disk_requests;
  140.     extern int    disk_requests_out;
  141.     extern int    threads_awaiting_q;
  142.     int                            blocked = 0; /* was, is blocked or will block */
  143.     register    LINK            *diskLink = volRec->volLink;
  144.  
  145.     disk_requests++;
  146.  
  147.     if(fastPath(volRec, message)) {
  148.         fast_path_taken++;
  149.         return Active->error;
  150.     }
  151.     slow_path_taken++;
  152.  
  153.     {
  154.         extern BOOL FastPathOnly;
  155.         SM_ASSERT(LEVEL_1, (FastPathOnly == FALSE));
  156.     }
  157.     SM_ASSERT(LEVEL_1, (diskLink != NULL) );
  158.  
  159.     TRPRINT(TR_THREAD, TR_LEVEL_1, 
  160.         ("(Thread %d state %d) callDisk disk on vol %d",
  161.         Active->id, Active->state,  volRec->queues->semNum));
  162.  
  163.     TRPRINT(TR_DISK, TR_LEVEL_2, ("callDisk link %d", diskLink->id));
  164.  
  165. #ifdef TIME_CALLDISK
  166.     btime(&beginning); 
  167. #endif TIME_CALLDISK
  168.  
  169.  
  170.     /*
  171.      *    setup the thread id in the message to be used
  172.      *    to dispatch the waiting thread upon receipt
  173.      */
  174.     message->body.threadId = Active->id;
  175.     message->diskmagic = DISK_MESSAGE_MAGIC;
  176.  
  177.     /*
  178.      *    send the message: we have two implementations:
  179.      *  1 using shared-memory queues and the other using sockets.
  180.      */
  181.     /* 
  182.      * When using disk queues, we don't want stop-and-wait
  183.      * behavior so we don't use the semaphore here.
  184.      * Instead, we wait only if the disk queue is full.
  185.      */
  186.      getMutex(&(volRec->queues->toDisk.mutex));
  187.      /*
  188.       * Find out if the other process is blocked on a semaphore,
  189.       * waiting for this queue to be non-empty. If so, we'll want to kick
  190.       * the disk process before we're done but when we're out of this
  191.       * critical section.
  192.       */
  193.      if(toDiskFull(volRec->queues)) {
  194.         giveMutex(&(volRec->queues->toDisk.mutex));
  195.         /* 
  196.          * ok, we can't put it on the queue yet, so put this thread
  197.          * to sleep and run another 
  198.          * (This thread is awakened in  selectDisk().)
  199.          */
  200.         threads_awaiting_q++;
  201.  
  202. #define diskTakingRequests     tcbList
  203.         waitList( &(volRec->diskTakingRequests), THREAD_DISK_Q_WAIT );
  204.         getMutex(&(volRec->queues->toDisk.mutex));
  205.      }
  206.  
  207.      enq_to(volRec->queues,  ShmAddressToOffset(message));
  208.      blocked = volRec->queues->toDisk.mutex.willBlock;
  209.      giveMutex(&(volRec->queues->toDisk.mutex));
  210.  
  211.      TRPRINT(TR_DISK|TR_DISKRW, TR_LEVEL_1, 
  212.          ("thread %d msgtype %d -> disk %d", Active->id, 
  213.             message->header.type, volRec->queues->semNum));
  214.  
  215.      if(blocked) {
  216.             struct     sembuf  _sembuf;
  217.  
  218.             _sembuf.sem_num = volRec->queues->semNum;
  219.             _sembuf.sem_op  =  1;  /* a semaphore V operation */
  220.             _sembuf.sem_flg = 0;
  221.             TRPRINT(TR_DISK|TR_DISKRW, TR_LEVEL_1, 
  222.                 ("V on SYSV semaphore # %d", volRec->queues->semNum));
  223.  
  224.             serv_v_ops++;
  225.             if( semop(SemId, &_sembuf, 1) < 0 ) {
  226.                 if(errno != EINTR) {
  227.                     SM_ERROR(TYPE_SYS, errno);
  228.                 }
  229.             }
  230.      }
  231.     TRPRINT(TR_DISK|TR_THREAD, TR_LEVEL_1, 
  232.         ("waiting for disk %d to respond", volRec->queues->semNum));
  233.  
  234.  
  235.     /*
  236.      *    wait on the list for completion of the request
  237.      * (This thread is awakened in  diskReceiveQ().)
  238.      */
  239.     disk_requests_out++;
  240.  
  241. #define diskGivingResults diskLink->tcbList
  242.     waitList( &(diskGivingResults), THREAD_DISK_WAIT );
  243.     
  244.     TRPRINT(TR_DISK|TR_THREAD, TR_LEVEL_1, ("call result:%d", Active->error));
  245.  
  246. #ifdef TIME_CALLDISK
  247.     etime(&beginning); 
  248. #endif TIME_CALLDISK
  249.  
  250.     return(Active->error);
  251. }
  252.